<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */

    /**
     * ErrorBaseValidator represents a validation error.
     * bases on the sfValidatorError of symfony framework.
     *
     * @package    kassiop.system.validators
     * @subpackage base
     *
     * @author NairuS
     * @version 1.3 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     */
    class ErrorBaseValidator extends Exception implements Serializable
    {
        /**
         * The instance of the validator.
         *
         * @var BaseValidator
         */
        protected $validator = null ;

        /**
         * The array of arguments.
         *
         * @var array
         */
        protected $arguments = array() ;

        /**
         * Constructor.
         *
         * @param BaseValidator $validator  An BaseValidator instance
         * @param string        $code       The error code
         * @param array         $arguments  An array of named arguments needed to render the error message
         */
        public function __construct( BaseValidator $validator, $code, array $arguments = array() )
        {
            $this->validator = $validator ;
            $this->arguments = $arguments ;

            // override default exception message and code
            $this->code = $code ;

            if( !$messageFormat = $this->getMessageFormat() )
            {
                $messageFormat = $code ;
            }
            $this->message = strtr( $messageFormat, $this->getArguments() ) ;
        }

        /**
         * Returns the arguments needed to format the message.
         *
         * @param bool $raw  false to use it as arguments for the message format, true otherwise (default to false)
         *
         * @see getMessageFormat()
         */
        public function getArguments( $raw = false )
        {
            if( $raw )
            {
                return $this->arguments ;
            }

            $arguments = array() ;
            foreach( $this->arguments as $key => $value )
            {
                if( is_array( $value ) )
                {
                    continue ;
                }
                $arguments["%$key%"] = htmlspecialchars( $value, ENT_QUOTES, BaseValidator::getCharset() ) ;
            }
            return $arguments ;
        }

        /**
         * Returns the message format for this error.
         *
         * If no message format has been set in the validator, the exception standard
         * message is returned.
         *
         * @return string The message format
         */
        public function getMessageFormat()
        {
            $messageFormat = $this->validator->getMessage( $this->code ) ;
            if( !$messageFormat )
            {
                $messageFormat = $this->getMessage() ;
            }

            return $messageFormat;
        }

        /**
         * Returns the input value that triggered this error.
         *
         * @return mixed The input value
         */
        public function getValue()
        {
            return isset( $this->arguments['value'] ) ? $this->arguments['value'] : null ;
        }

        /**
         * Returns the validator that triggered this error.
         *
         * @return sfValidatorBase A sfValidatorBase instance
         */
        public function getValidator()
        {
            return $this->validator ;
        }

        /**
         * Serializes the current instance.
         *
         * We must implement the Serializable interface to overcome a problem with PDO
         * used as a session handler.
         *
         * The default serialization process serializes the exception trace, and because
         * the trace can contain a PDO instance which is not serializable, serializing won't
         * work when using PDO.
         *
         * @return string The instance as a serialized string
         */
        public function serialize()
        {
            return serialize( array( $this->validator, $this->arguments, $this->code, $this->message ) ) ;
        }

        /**
         * Unserializes a sfValidatorError instance.
         *
         * @param string $serialized  A serialized sfValidatorError instance
         *
         */
        public function unserialize($serialized)
        {
            list( $this->validator, $this->arguments, $this->code, $this->message ) = unserialize( $serialized ) ;
        }

        /**
         * Returns the string representation of the error.
         *
         * @return string The error message
         */
        public function __toString()
        {
            return $this->getMessage() ;
        }
    }
?>